package com.example.ywr.controller;

import com.example.ywr.common.PageParam;
import com.example.ywr.common.PageResult;
import com.example.ywr.common.Result;
import com.example.ywr.entity.User;
import com.example.ywr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("更新用户")
    @PutMapping("/{id}")
    public Result<User> update(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        User updatedUser = userService.update(user);
        if (updatedUser == null) {
            return Result.failed("用户不存在");
        }
        return Result.success(updatedUser);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = userService.delete(id);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("删除失败");
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = userService.batchDelete(ids);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量删除失败");
    }

    @ApiOperation("获取用户详情")
    @GetMapping("/{id}")
    public Result<User> getById(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.failed("用户不存在");
        }
        // 隐藏密码
        user.setPassword(null);
        return Result.success(user);
    }

    @ApiOperation("分页查询用户列表")
    @GetMapping
    public Result<PageResult<User>> list(PageParam pageParam) {
        PageResult<User> pageResult = userService.list(pageParam);
        // 隐藏密码
        pageResult.getList().forEach(user -> user.setPassword(null));
        return Result.success(pageResult);
    }

    @ApiOperation("根据用户名模糊查询")
    @GetMapping("/search/username")
    public Result<PageResult<User>> listByUsername(@RequestParam String username, PageParam pageParam) {
        PageResult<User> pageResult = userService.listByUsername(username, pageParam);
        // 隐藏密码
        pageResult.getList().forEach(user -> user.setPassword(null));
        return Result.success(pageResult);
    }

    @ApiOperation("根据昵称模糊查询")
    @GetMapping("/search/nickname")
    public Result<PageResult<User>> listByNickname(@RequestParam String nickname, PageParam pageParam) {
        PageResult<User> pageResult = userService.listByNickname(nickname, pageParam);
        // 隐藏密码
        pageResult.getList().forEach(user -> user.setPassword(null));
        return Result.success(pageResult);
    }

    @ApiOperation("根据角色查询用户")
    @GetMapping("/role/{role}")
    public Result<PageResult<User>> listByRole(@PathVariable String role, PageParam pageParam) {
        PageResult<User> pageResult = userService.listByRole(role, pageParam);
        // 隐藏密码
        pageResult.getList().forEach(user -> user.setPassword(null));
        return Result.success(pageResult);
    }

    @ApiOperation("更新用户状态")
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean success = userService.updateStatus(id, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("更新状态失败");
    }

    @ApiOperation("批量更新用户状态")
    @PutMapping("/batch/status/{status}")
    public Result<Boolean> batchUpdateStatus(@RequestBody List<Long> ids, @PathVariable Integer status) {
        boolean success = userService.batchUpdateStatus(ids, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量更新状态失败");
    }

    @ApiOperation("重置密码")
    @PutMapping("/{id}/password/reset")
    public Result<Boolean> resetPassword(@PathVariable Long id, @RequestParam String password) {
        boolean success = userService.resetPassword(id, password);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("重置密码失败");
    }
}